package cn.apthink.schedule;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import cn.apthink.calendar.Department;
import cn.apthink.calendar.DepartmentHelper;
import cn.apthink.utility.Util;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class EfficiencyHelper {
	private DepartmentHelper departmentHelper;
	private ProductHelper productHelper;
	private List<Efficiency> lstEfficiency;
	//是否强制生产效率表设置
	private boolean efficiencyStrict = false;
	//是否生产效率表设置数量级别
	private boolean efficiencyLevel = false;
	
	public EfficiencyHelper(DepartmentHelper departmentHelper, ProductHelper productHelper, List<Efficiency> lstEfficiency) {
		this.departmentHelper = departmentHelper;
		this.productHelper = productHelper;
		this.lstEfficiency = lstEfficiency;
	}
	
	private double getDepartmentEfficiency(String departmentId) {
		return efficiencyStrict?0:departmentHelper.get(departmentId).getEfficiency();
	}

	private boolean hasEfficiency(String departmentId, String productId) {
		if (Util.isEmpty(lstEfficiency))
			return false;
		Optional<Efficiency> o = lstEfficiency.stream().filter(x->{
			return departmentId.equals(x.getDepartmentId()) && productId.equals(x.getProductId());
		}).findAny();
		return o.isPresent();
	};
	
	private List<Efficiency> filterEfficiency(String departmentId, String productId) {
		return lstEfficiency.stream().filter(x->{
			return departmentId.equals(x.getDepartmentId()) && productId.equals(x.getProductId());
		}).collect(Collectors.toList());
	};

	private double getEfficiencyLevel(String departmentId, String productId, Integer amountLevel) {
		if (Util.isEmpty(amountLevel) && !efficiencyLevel) {
			return filterEfficiency(departmentId, productId).get(0).getEfficiency();
		} else {
			List<Efficiency> ds = filterEfficiency(departmentId, productId);
			ds.sort((a,b)->{
				return b.getAmount()-a.getAmount();
			});
			for (int i=0,l=ds.size();i<l;i++) {
				Efficiency e = ds.get(i);
				if (amountLevel>=e.getAmount()) {
					return e.getEfficiency();
				}
			}
			// 数量达不到最低标准时返回单位上缺省效率
			return getDepartmentEfficiency(departmentId);
		}
	}
	
	public double getEfficiency(String departmentId, String productId, Integer amountLevel) {
		if (Util.isEmpty(lstEfficiency)) {
			return getDepartmentEfficiency(departmentId);
		}
		// 递归查询此产品在单位树上是否预设效率
		String pid = productId;
		while (!Util.isEmpty(pid)) {
			String did = departmentId;
			while (!Util.isEmpty(did)) {
				if (hasEfficiency(did, pid)) {
					return getEfficiencyLevel(did, pid, amountLevel);
				}
				did = departmentHelper.getPid(did);
			}
			pid = productHelper.getPid(pid);
		}
		// 没有预设效率
		return getDepartmentEfficiency(departmentId);
	}
	
	public static EfficiencyHelper testData() {
		ProductHelper productHelper = new ProductHelper(Product.testDatas());
		DepartmentHelper departmentHelper = new DepartmentHelper(Department.testDatas());
		EfficiencyHelper helper = new EfficiencyHelper(departmentHelper, productHelper, Efficiency.testDatas());
		return helper;
		
	}
}
